import { defineStore } from 'pinia'
import * as api from '@/api/menu'

export const menuStore = defineStore('menu', {
  state: () => ({
    menuNav: [],
    menuAuth: [],
    menuTree: [],
    menuInfo: {},
    pagination: {
      page: 1,
      limit: 10,
      total: 0
    }
  }),
  actions: {
    async fetchMenuNav() {
      const res = await api.getMenuNav()
      console.log(res)
      this.menuNav = res.list || []
    },
    async fetchMenuAuth() {
      const res = await api.getMenuAuth()
      this.menuAuth = res.list || []
    },
    async fetchMenuTree(param) {
      const res = await api.getMenuTree(param)
    //   console.log(res)
      this.menuTree = res || []
    },
    async fetchMenuInfo(id) {
      const res = await api.getMenuInfo(id)
      return res || {}
    },
    async addMenu(data) {
      await api.addMenu(data)
    },
    async editMenu(data) {
      await api.editMenu(data)
    },
    async saveMenu(data) {
      return data.id ? await api.editMenu(data) : await api.addMenu(data)
    },
    async deleteMenu(id) {
      const res = await api.deleteMenu(id)
    }
  }
})